package com.sgcc.ln.pmos.mobileapp.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvcNetCallback;

import com.sgcc.ln.pmos.mobileapp.bean.AccountInfoBean;
import com.sgcc.ln.pmos.mobileapp.bean.CodeBean;
import com.sgcc.ln.pmos.mobileapp.bean.PowLevelAndTypeBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 新增户号-----增量配电网弹窗
 */
public class AccountDetailPopup extends CenterPopupView {
    private LinearLayout linner_type;
    private TextView tv_delete, tv_clos;
    private TextView tv_voltagegrade, tv_electricitytype, tv_pricetype, tv_mvoltcode, tv_electricitytype_2;
    private TextView eidt_consno, edit_marketingusername, edit_address, edit_membersnamepointid, edit_pointname, edit_mpcap, edit_lastyearpq;

    Map<String, Object> params = new HashMap<>();
    List<PowLevelAndTypeBean.DataDTO> powLevel = new ArrayList<>();
    List<PowLevelAndTypeBean.DataDTO> powType = new ArrayList<>();
    String[] powLevel_show;
    String[] powType_show;
    String[] pricetypes;
    private AccountInfoBean.DataDTO bean;

    public AccountDetailPopup(@NonNull Context context, OnClickListener onClickListener,
                              AccountInfoBean.DataDTO bean) {
        super(context);
        this.onClickListener = onClickListener;
        this.bean = bean;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.account_detail_pop;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        String[] strings = new String[3];
        strings[0] = "国家电网";
        strings[1] = "地方电网";
        strings[2] = "增量配电网";

        pricetypes = new String[2];
        pricetypes[0] = "单一制";
        pricetypes[1] = "两部制";
        initTextAndEdit();
        tv_delete = findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean != null) {
                    deletMeterPointInfo(bean.getUsePointId());
                }
            }
        });
        tv_clos = findViewById(R.id.tv_clos);
        tv_clos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        linner_type = findViewById(R.id.linner_type);
        linner_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new XPopup.Builder(getContext())
                        .hasShadowBg(false)
                        .atView(view)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                        .asAttachList(strings,
                                null,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
//                                        if (onClickListener != null) {
//                                            onClickListener.onTypeClick(AccountDetailPopup.this, position);
//                                        }
                                    }
                                }, 0, 0, Gravity.CENTER).show();
            }
        });
    }

    private void initTextAndEdit() {
        tv_voltagegrade = findViewById(R.id.tv_voltagegrade);
        tv_electricitytype = findViewById(R.id.tv_electricitytype);
        tv_pricetype = findViewById(R.id.tv_pricetype);
        tv_mvoltcode = findViewById(R.id.tv_mvoltcode);
        tv_electricitytype_2 = findViewById(R.id.tv_electricitytype_2);

        eidt_consno = findViewById(R.id.eidt_consno);
        edit_marketingusername = findViewById(R.id.edit_marketingusername);
        edit_address = findViewById(R.id.edit_address);
        edit_membersnamepointid = findViewById(R.id.edit_membersnamepointid);
        edit_pointname = findViewById(R.id.edit_pointname);
        edit_mpcap = findViewById(R.id.edit_mpcap);
        edit_lastyearpq = findViewById(R.id.edit_lastyearpq);

        if (bean != null) {
            tv_voltagegrade.setText(TextUtils.isEmpty(bean.getVoltageGrade()) ? " " : bean.getVoltageGrade());
            tv_electricitytype.setText(TextUtils.isEmpty(bean.getElectricityType()) ? " " : bean.getElectricityType());
            tv_pricetype.setText(TextUtils.isEmpty(bean.getPrice()) ? " " : bean.getPrice());
            tv_mvoltcode.setText(TextUtils.isEmpty(bean.getMvoltCode()) ? " " : bean.getMvoltCode());
            tv_electricitytype_2.setText(TextUtils.isEmpty(bean.getElectricityType()) ? " " : bean.getElectricityType());

            eidt_consno.setText(TextUtils.isEmpty(bean.getConsNo()) ? " " : bean.getConsNo());
            edit_marketingusername.setText(TextUtils.isEmpty(bean.getMarketingUserName()) ? " " : bean.getMarketingUserName());
            edit_address.setText(TextUtils.isEmpty(bean.getAddress()) ? " " : bean.getAddress());
            edit_membersnamepointid.setText(TextUtils.isEmpty(bean.getMembersNamepointId()) ? " " : bean.getMembersNamepointId());
            edit_pointname.setText(TextUtils.isEmpty(bean.getPointName()) ? " " : bean.getPointName());
            edit_mpcap.setText(TextUtils.isEmpty(bean.getMpCap()) ? " " : bean.getMpCap());
            edit_lastyearpq.setText(TextUtils.isEmpty(bean.getLastyearPq()) ? " " : bean.getLastyearPq());

        }

    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .8f);
    }

    @Override
    protected int getMaxWidth() {
        return (int) (XPopupUtils.getScreenWidth(getContext()) * .9f);
    }

    @Override
    protected boolean onBackPressed() {
        return true;
    }


    public void deletMeterPointInfo(String usePointId) {
        Map<String, Object> params = new HashMap<>();
        params.put("usePointId", usePointId);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.DELET_METERPOINTINFO, new MvcNetCallback<CodeBean>(true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
            }

            @Override
            public void onResponse(CodeBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if (response.getStatus() == 0) {
                        ToastUtils.showShort("删除成功");
                        if (null != onClickListener)
                            onClickListener.onDeletSuccess();
                        dismiss();
                    } else {
                        ToastUtils.showShort(response.getMessage());
                    }
                } else {
                    ToastUtils.showShort("删除失败");
                }
            }
        });
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickListener onClickListener;


    public interface OnClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onDeletSuccess();


    }
}
